package chapter4.No4_1_使用reentrantLock类.demo7;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * !
 *
 * @author liujian
 * @version 2016/11/21 0021
 */
public class ServiceClass {
    ReentrantLock lock = new ReentrantLock();
    Condition conditionA = lock.newCondition();
    Condition conditionB = lock.newCondition();

    public void methodA() {
        lock.lock();
        System.out.println("A方法开始执行");
        try {
            conditionA.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("A方法结束执行");
        lock.unlock();
    }

    public void methodB() {
        lock.lock();
        System.out.println("B方法开始执行");
        try {
            conditionB.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("B方法结束执行");
        lock.unlock();
    }

    public void signalA() {
        lock.lock();
        System.out.println("唤醒A"+System.currentTimeMillis());
        conditionA.signal();
        lock.unlock();
    }

    public void signalB() {
        lock.lock();
        System.out.println("唤醒B"+System.currentTimeMillis());
        conditionB.signal();
        lock.unlock();
    }


}
